package com.jlz.beans.def;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jlz.managers.def.Columns;
import com.jlz.managers.def.Valuetypes;
import com.julewa.db.DB;
import com.julewa.db.Entity;

/*************************************
 * 字段表
 * 重点在于根据type的不同,其性质将不同 
 *************************************/
@DB.USECACHE
@Component @Scope(Entity.SCOPE)
public class ColumnBean extends Entity<Long> implements Comparable<ColumnBean>{
	
	//从属的类型
	private @DB.COLUMN long parentId = -1;
	private @DB.COLUMN(min=0) int idx = 0;//顺序
 	private @DB.COLUMN(min=2,max=64)  @DB.AUTOTRIM String code; //这个用以在编码中做商业逻辑处理
	private @DB.COLUMN(min=2,max=128) @DB.AUTOTRIM String label; //显示的中文名
	
	//数据类型,这会更加具体,将涉及到数值范围和有效性检查 
	private @DB.COLUMN int valuetypeId = Valuetypes.TYPE_ID_NODE; 
	private @DB.COLUMN int arraySize = 1;
 	
	//
	private @DB.COLUMN(min=0,max=100) int visibleType = Columns.VISIBLE.DISPLAY.getValue();//可见级别: 列表可见,内容页可见,编辑可见,不可见
	private @DB.COLUMN(min=0,max=100) int editType = Columns.EDITABLE.ENABLE.getValue();//不可编辑,可编辑,必须填写
	private @DB.COLUMN(min=5,max=800) int displaySize = 200; //input框显示的字符长度
	private @DB.COLUMN(min=0,max=1024) String defaultValue = "";//缺省值的字符类型
	
	// 
	private @Autowired Valuetypes valuetypes = null;
	private @Autowired Columns columns = null; 
	private @DB.DEPENDON("valuetypeId") ValuetypeBean valuetype = null;
	private @DB.DEPENDON("parentId") ColumnBean parent = null; 
	//
	private @DB.DEPENDON("arraySize") List<ColumnBean> children = null;
	private @DB.DEPENDON("code") String fullCode = null;
	//
	
	//
	public String getFullCode(){
		if(fullCode != null) return fullCode;
		if(this.parentId < 1){
			this.fullCode = "";
		}else{
			this.fullCode = this.getParentColumn().getFullCode() + "." + this.code;
		}
		return this.fullCode;
	}
	//
	public List<ColumnBean> getChildren(){
		if(children == null){
			children = columns.getColumnsOf(this);
		}
		return children;
	}
 
	public ColumnBean createChild(){
		ColumnBean col = columns.create( );
		col.parentId = this.getID();
		col.idx = this.getChildren().size(); 
		return col;
	}
	
	public boolean appendChild(ColumnBean column){
		if(!columns.insert(column))return false;
		if(this.children != null){
			this.children.add(column);
			Collections.sort(children);
		}
		return true;
	}
	public boolean updateChild(ColumnBean column){
		if(!columns.update(column)) return false;
		if(this.children != null){
			Collections.sort(children);
		}
		return true;
	}
	public boolean removeChild(ColumnBean column){
		if(!columns.remove(column)) return false;
		if(this.children != null){
			this.children.remove(column);
			Collections.sort(children);
		}
		return true;
	}
 	//
	public boolean isPrimary(){
		return this.arraySize == 1;
	}
	public boolean isComplex(){
		return this.valuetypeId > Valuetypes.TYPE_ID_NODE;
	}
	
	public boolean isInArray(){
		if(this.getParentColumn() == null)
			return false;
		if(this.parent.isInArray()){
			return true;
		}
		return this.parent.arraySize != 0;
	}
	//强制将array以下类型设置为not array,这样无论array值设成什么样都是无效的
	public boolean isArray(){
		if(this.isInArray())return false;
		return this.arraySize != 1;
	}
	public boolean isInfinite(){
		return this.arraySize < 1;
	}
	//
	private Columns.EDITABLE editable = null;
	public Columns.EDITABLE getEditable(){
		if(editable == null){
			editable = Columns.EDITABLE.of(editType);
		}
		return editable;
	}
	
	private Columns.VISIBLE visible = null;
	public Columns.VISIBLE getVisible(){
		if(visible == null){
			visible = Columns.VISIBLE.of(visibleType);
		}
		return visible;
	}
	
	// 
	public ValuetypeBean getValuetype() {
		if(valuetype == null && valuetypeId > 0){
			valuetype = valuetypes.get(valuetypeId);
		}
		return valuetype;
	}
	public ColumnBean getParentColumn(){
		if(parent == null){
			if(parentId < 0)return null;
			parent = columns.get(parentId);
		}
		return parent;
	}
	//
	
	@Override
	public int compareTo(ColumnBean bean) {
		return this.idx - bean.idx;
	}

	public long getParentId() {
		return parentId;
	}

	public void setParentId(long parentId) {
		this.parentId = parentId;
	}

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public int getValuetypeId() {
		return valuetypeId;
	}

	public void setValuetypeId(int valuetypeId) {
		this.valuetypeId = valuetypeId;
	}

	public int getArraySize() {
		return arraySize;
	}

	public void setArraySize(int arraySize) {
		this.arraySize = arraySize;
	}

	public int getVisibleType() {
		return visibleType;
	}

	public void setVisibleType(int visibleType) {
		this.visibleType = visibleType;
	}

	public int getEditType() {
		return editType;
	}

	public void setEditType(int editType) {
		this.editType = editType;
	}

	public int getDisplaySize() {
		return displaySize;
	}

	public void setDisplaySize(int displaySize) {
		this.displaySize = displaySize;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
  
}
